#include "init.h"



int d2im_init(){

    /* 所有文件类信息的读取和初始化
     * 以后再支持UTF-8吧
     * 内存, 状态初始化不出现在这个函数中
     * 而是放到run函数的while之前处理
     */
    const char  *tmp_str;
    als_cfg     *tmp_cfg;

    int         found, i, tmp_int;

    static struct{
        char   *key;       /* 注意这里只能是常量, 不允许是变量 */
        BYTE    type;
        void   *ptr;       /* 而这里只能是变量, 不能是常量 */
        void   *def;       /* 注意这里只能是常量, 不允许是变量 */
    }datas[] = {
        {("FULLSCREEN"),     CFG_BOOL,     &(glb_d2im.cfg.full_screen),      (void*)("NO"           )},
        {("WIDTH"     ),     CFG_INT ,     &(glb_d2im.cfg.width      ),      (void*)(800            )},
        {("HEIGHT"    ),     CFG_INT ,     &(glb_d2im.cfg.height     ),      (void*)(600            )},
        {("FPS"       ),     CFG_INT ,     &(glb_d2im.cfg.fps        ),      (void*)(30             )},
        {("TITLE"     ),     CFG_STR ,     &(glb_d2im.cfg.title      ),      (void*)("D2IM"    )},
        {(""          ),     CFG_NULL,     NULL,                             (void*)(0              )},
    };


    DEBUG_MESSAGE("d2im_init():in\n");

    srand(time(NULL));
    if(!al_init()){
        FATAL_EXIT("could not init allegro ...\n");
    }
    if(!al_install_mouse()){
        FATAL_EXIT("could not install mouse ...\n");
    }
    if(!al_install_keyboard()){
        FATAL_EXIT("could not install keyboard ...\n");
    }

    glb_d2im.cfg.cfg_file = al_load_config_file("./d2im.cfg");
    if( !glb_d2im.cfg.cfg_file ){
        DEBUG_MESSAGE("no configure file, try to create one\n");
        /* 开始 */
    }

    i = 0;
    tmp_cfg = glb_d2im.cfg.cfg_file;
    while (datas[i].type != CFG_NULL) {
        /* 不要自己去获得最佳值, 有个默认值就够了 */
        /* 万一生成的configure file被非法修改了, 这里必须考虑到 */
        found = 1;
        tmp_str = al_get_config_value(tmp_cfg, NULL, datas[i].key);

        if(!tmp_str){
            fprintf( stderr, "waring: key not found : <%s>\n", datas[i].key);
            found = 0;
        }

        switch (datas[i].type) {

            case CFG_STR:
                *((char**)datas[i].ptr) = ((found && strlen(tmp_str))?(char*)tmp_str:(char*)datas[i].def);
                DEBUG_MESSAGE("<  %-12s> = %s\n", datas[i].key, *((char**)datas[i].ptr));
                break;

            case CFG_INT:
                tmp_int = atoi(tmp_str);
                /* 只能处理大于0的整数,其他都会使用默认值
                 * 以后改进
                 */
                *((int*)datas[i].ptr) = ((found && tmp_int)?tmp_int:(int)datas[i].def);
                DEBUG_MESSAGE("<  %-12s> = %d\n", datas[i].key, *((int*)datas[i].ptr));
                break;

            case CFG_BOOL:
                if(found){
                    if(!strcasecmp(tmp_str,"YES")||!strcasecmp(tmp_str,"Y")||!strcasecmp(tmp_str,"TRUE")||!strcasecmp(tmp_str,"T")||!strcasecmp(tmp_str,"1")){
                        *((int*)datas[i].ptr) = 1;
                    }else if(!strcasecmp(tmp_str,"NO")||!strcasecmp(tmp_str,"N")||!strcasecmp(tmp_str,"FALSE")||!strcasecmp(tmp_str,"F")||!strcasecmp(tmp_str,"0")){
                        *((int*)datas[i].ptr) = 0;
                    }else{
                        *((int*)datas[i].ptr) = (int)datas[i].def;
                    }
                }else{
                    *((int*)datas[i].ptr) = (int)datas[i].def;
                }
                DEBUG_MESSAGE("<  %-12s> = %d\n", datas[i].key, *((int*)datas[i].ptr));
                break;
            default:
                break;
        }
        i++;
    }



    glb_d2im.disp = al_create_display(glb_d2im.cfg.width, glb_d2im.cfg.height);
    if(!glb_d2im.disp){
        fprintf(stderr, "could not create display: %d*%d ...\n", glb_d2im.cfg.width, glb_d2im.cfg.height);
        return ERROR_CREATE_DISPLAY;
    }


    glb_d2im.timer = al_create_timer(1.0/glb_d2im.cfg.fps);
    if(!glb_d2im.timer){
        fprintf(stderr, "could not create timer ...\n");
        return ERROR_CREATE_TIMER;
    }


    glb_d2im.evtq = al_create_event_queue();
    if(!glb_d2im.evtq){
        FATAL_EXIT("could not create event queue.\n");
    }

    al_register_event_source(glb_d2im.evtq, al_get_keyboard_event_source());
    al_register_event_source(glb_d2im.evtq, al_get_mouse_event_source());
    al_register_event_source(glb_d2im.evtq, al_get_display_event_source(glb_d2im.disp));
    al_register_event_source(glb_d2im.evtq, al_get_timer_event_source(glb_d2im.timer));

    al_set_window_title(glb_d2im.disp, glb_d2im.cfg.title);


    DEBUG_MESSAGE("d2im_init():out\n");
    return 0;

}
